package com.project.platform.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.project.platform.dto.CurrentUserDTO;
import com.project.platform.dto.RetrievePasswordDTO;
import com.project.platform.dto.UpdatePasswordDTO;
import com.project.platform.entity.Shop;
import com.project.platform.exception.CustomException;
import com.project.platform.mapper.ShopMapper;

import com.project.platform.service.ShopService;

import com.project.platform.utils.CurrentUserThreadLocal;
import com.project.platform.vo.PageVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;




    /**
     * 店铺
     */
    @Service
    public class ShopServiceImpl implements ShopService {
        @Resource
        private ShopMapper shopMapper;
        @Value("${resetPassword}")
        private String resetPassword;

        @Override
        public PageVO<Shop> page(Map<String, Object> query, Integer pageNo, Integer pageSize) {
            PageVO<Shop> page = new PageVO();
            List<Shop> list = shopMapper.queryPage((pageNo - 1) * pageSize, pageSize, query);
            page.setList(list);
            page.setTotal(shopMapper.queryCount(query));
            return page;
        }

        @Override
        public Shop selectById(Integer id) {
            Shop shop = shopMapper.selectById(id);
            return shop;
        }

        @Override
        public List<Shop> list() {
            return shopMapper.list();
        }
        @Override
        public void insert(Shop shop) {
            check(shop);
            if (shop.getPassword() == null) {
                shop.setPassword(resetPassword);
            }
            shop.setFansCount(0);//设置初始粉丝为0
            shopMapper.insert(shop);
        }
        @Override
        public void update(Shop shop) {
            check(shop);
            shopMapper.update(shop);
        }
        private void check(Shop shop) {
            Shop shopUsername = shopMapper.selectByUsername(shop.getUsername());
            if (shopUsername != null && shopUsername.getId() != shop.getId()) {
                throw new CustomException("用户名已存在");
            }
        }

        @Override
        public void delete(List<Integer> ids) {
            shopMapper.delete(ids);
        }


        @Override
        public CurrentUserDTO login(String username, String password) {
            Shop shop = shopMapper.selectByUsername(username);
            if (shop == null || !shop.getPassword().equals(password)) {
                throw new CustomException("用户名或密码错误");
            }
            if (shop.getStatus().equals("禁用")) {
                throw new CustomException("用户已禁用，新注册的用户请等待管理员审核启用");
            }
            CurrentUserDTO currentShopDTO = new CurrentUserDTO();
            BeanUtils.copyProperties(shop, currentShopDTO);
            return currentShopDTO;
        }

        @Override
        public void register(JSONObject data) {
            Shop shop = new Shop();
            shop.setUsername(data.getString("username"));
            shop.setNickname(data.getString("nickname"));
            shop.setAvatarUrl(data.getString("avatarUrl"));
            shop.setPassword(data.getString("password"));
            shop.setAptitudeImgs(data.getString("aptitudeImgs"));
            shop.setName(data.getString("name"));
            shop.setStatus("禁用");//默认禁用，需要管理员审核
            insert(shop);
        }


        @Override
        public void updateCurrentUserInfo(CurrentUserDTO currentShopDTO) {
            Shop shop = shopMapper.selectById(currentShopDTO.getId());
            shop.setId(currentShopDTO.getId());
            shop.setNickname(currentShopDTO.getNickname());
            shop.setAvatarUrl(currentShopDTO.getAvatarUrl());
            shop.setTel(currentShopDTO.getTel());
            shop.setEmail(currentShopDTO.getEmail());
            shopMapper.update(shop);
        }

        @Override
        public void updateCurrentUserPassword(UpdatePasswordDTO updatePassword) {
            Shop shop = shopMapper.selectById(CurrentUserThreadLocal.getCurrentUser().getId());
            if (!shop.getPassword().equals(updatePassword.getOldPassword())) {
                throw new CustomException("旧密码不正确");
            }
            shop.setPassword(updatePassword.getNewPassword());
            shopMapper.update(shop);
        }

        @Override
        public void resetPassword(Integer id) {
            Shop shop = shopMapper.selectById(id);
            shop.setPassword(resetPassword);
            shopMapper.update(shop);
        }

        @Override
        public void retrievePassword(RetrievePasswordDTO retrievePasswordDTO) {
            Shop shop = shopMapper.SelbyTelphone(retrievePasswordDTO.getTel());
            if (shop == null) {
                throw new CustomException("手机号不存在");
            }
            //TODO 校验验证码
            shop.setPassword(retrievePasswordDTO.getPassword());
            shopMapper.update(shop);
        }



    }



